#include "check.h"
#include "ui_check.h"

check::check(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::check)
{
    ui->setupUi(this);

    this->setStyleSheet("background-color: #E2EEF7");
    ui->pushButton_change->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_del->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_Screen->setStyleSheet("color: white; background-color: #55AAFF");
    this->setWindowTitle("儿童信息设置");

    sql = QSqlDatabase::addDatabase("QSQLITE", "third");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        qDebug() << "数据库连接成功";
    }
    else{
        qDebug() << "数据库连接失败";
    }
    ui->pushButton_change->setCheckable(true);
    connect(ui->pushButton_change, SIGNAL(toggled(bool)), this, SLOT(can_change(bool)));
    connect(ui->pushButton_del, SIGNAL(clicked(bool)), this, SLOT(close()));
    connect(ui->pushButton_del, SIGNAL(clicked(bool)), this, SLOT(del_info()));
    connect(ui->pushButton_Screen, SIGNAL(clicked(bool)), this, SLOT(click_screen()));
}


void check::get_id(int id){
    QSqlQuery query(sql);
    set_menu();
    this->id = id;
    QString sql_text = tr("select * from child_data where id=") + QString::number(id);
    qDebug() << sql_text;
    query.exec(sql_text);

    query.next();
    QString str_tit = tr("儿童基本信息 id : ") + QString::number(id);
    str_tit +=  tr("姓名 : ");
    str_tit += query.value(1).toString();
    ui->label_title->setText(str_tit);
    //名字
    ui->lineEdit_name->setText(query.value(1).toString());

    //性别
    if(query.value(2).toInt() == 0){
        ui->radioButton_male->setChecked(false);
        ui->radioButton_female->setChecked(true);
    }
    else{
        ui->radioButton_male->setChecked(true);
        ui->radioButton_female->setChecked(false);
    }


    //出生日期
    QString date = query.value(3).toString();
    QStringList date_list = date.split('/');
    QDate time(date_list[0].toInt(), date_list[1].toInt(), date_list[2].toInt());
    ui->dateEdit_date->setDate(time);

    //年级
    int grade = query.value(4).toInt();
    ui->comboBox_grade->setCurrentIndex(grade);

    //电话
    QString phone = query.value(5).toString();
    ui->lineEdit_phone->setText(phone);

    //家庭类型
    int family = query.value(6).toInt();
    ui->comboBox_famliy->setCurrentIndex(family);

    //父亲文化
    int father_l = query.value(7).toInt();
    ui->comboBox_f_lv->setCurrentIndex(father_l);

    //母亲文化
    int mother_l = query.value(8).toInt();
    ui->comboBox_m_lv->setCurrentIndex(mother_l);

    //父亲职业
    int father_w = query.value(9).toInt();
    ui->comboBox_f_w->setCurrentIndex(father_w);

    //母亲职业
    int mother_w = query.value(10).toInt();
    ui->comboBox_m_w->setCurrentIndex(mother_w);

    set_ReadOnly();

    this->show();
}

void check::click_screen(){
    emit show_screen(this->id, ui->lineEdit_name->text());
}

void check::set_menu(){
    ui->comboBox_famliy->addItem(tr("核心家庭"));
    ui->comboBox_famliy->addItem(tr("主干家庭"));
    ui->comboBox_famliy->addItem(tr("联合家庭"));
    ui->comboBox_famliy->addItem(tr("单亲家庭"));
    ui->comboBox_famliy->addItem(tr("重组家庭"));
    ui->comboBox_famliy->addItem(tr("丁克家庭"));

    ui->comboBox_grade->addItem(tr("一年级"));
    ui->comboBox_grade->addItem(tr("二年级"));
    ui->comboBox_grade->addItem(tr("三年级"));
    ui->comboBox_grade->addItem(tr("四年级"));
    ui->comboBox_grade->addItem(tr("五年级"));
    ui->comboBox_grade->addItem(tr("六年级"));
    ui->comboBox_grade->addItem(tr("七年级"));
    ui->comboBox_grade->addItem(tr("八年级"));
    ui->comboBox_grade->addItem(tr("九年级"));

    QStringList w_list;
    w_list.append(tr("国家机关党群组织"));
    w_list.append(tr("专业技术人员"));
    w_list.append(tr("办事人员和有关人员"));
    w_list.append(tr("商业服务业人员"));
    w_list.append(tr("农林牧渔水利"));
    w_list.append(tr("生产运输"));
    w_list.append(tr("军人"));
    w_list.append(tr("不便分类"));
    w_list.append(tr("无职业"));

    QStringList e_list;
    e_list.append(tr("博士"));
    e_list.append(tr("硕士"));
    e_list.append(tr("本科"));
    e_list.append(tr("大专"));
    e_list.append(tr("中专与中技"));
    e_list.append(tr("高中"));
    e_list.append(tr("初中"));
    e_list.append(tr("小学"));
    e_list.append(tr("文盲与半文盲"));

    ui->comboBox_f_w->addItems(w_list);
    ui->comboBox_m_w->addItems(w_list);
    ui->comboBox_f_lv->addItems(e_list);
    ui->comboBox_m_lv->addItems(e_list);

    QButtonGroup btn_group;
    btn_group.addButton(ui->radioButton_female);
    btn_group.addButton(ui->radioButton_male);
    btn_group.setExclusive(true);



}


void check::del_info(){
    QString del_str;
    del_str = "DELETE FROM child_data WHERE id=" + QString::number(id);
    qDebug() << "del_str =  " << del_str;
    QSqlQuery query(sql);
    bool ok = query.exec(del_str);
    if(ok == true){
        QMessageBox::information(this, "删除成功", "删除成功", "确认", "取消");
    }
    else{
        QMessageBox::warning(this, "删除失败", "删除失败", "确认", "取消");
    }

}





void check::set_ReadOnly(){
    ui->lineEdit_name->setReadOnly(true);
    ui->dateEdit_date->setReadOnly(true);
    ui->comboBox_grade->setEnabled(false);
    ui->lineEdit_phone->setEnabled(false);
    ui->comboBox_f_lv->setEnabled(false);
    ui->comboBox_f_w->setEnabled(false);
    ui->comboBox_m_lv->setEnabled(false);
    ui->comboBox_m_w->setEnabled(false);
    ui->comboBox_famliy->setEnabled(false);
    ui->radioButton_female->setEnabled(false);
    ui->radioButton_male->setEnabled(false);
}


void check::can_change(bool ok){
    if(ok == true){
        ui->pushButton_change->setText("保存");
        ui->lineEdit_name->setReadOnly(!true);
        ui->dateEdit_date->setReadOnly(!true);
        ui->comboBox_grade->setEnabled(!false);
        ui->lineEdit_phone->setEnabled(!false);
        ui->comboBox_f_lv->setEnabled(!false);
        ui->comboBox_f_w->setEnabled(!false);
        ui->comboBox_m_lv->setEnabled(!false);
        ui->comboBox_m_w->setEnabled(!false);
        ui->comboBox_famliy->setEnabled(!false);
        ui->radioButton_female->setEnabled(!false);
        ui->radioButton_male->setEnabled(!false);
    }
    else{
        ui->pushButton_change->setText("修改");
        QSqlQuery query(sql);
        QString insert_text;
        insert_text += "name='" + ui->lineEdit_name->text() + "',";
        insert_text += "birth='" + ui->dateEdit_date->text() + "',";
        if(ui->radioButton_male->isChecked() == true){
            insert_text += "sex=1,";
        }
        if(ui->radioButton_female->isChecked() == true){
            insert_text += "sex=0,";
        }
        insert_text += "family=" + QString::number(ui->comboBox_famliy->currentIndex()) + ",";
        insert_text += "phone='" + ui->lineEdit_phone->text() + "',";
        insert_text += "Father_edu_level=" + QString::number(ui->comboBox_f_lv->currentIndex()) + ",";
        insert_text += "Mother_edu_level=" + QString::number(ui->comboBox_m_lv->currentIndex()) + ",";
        insert_text += "Father_occ=" + QString::number(ui->comboBox_f_w->currentIndex()) + ",";
        insert_text += "Mother_occ=" + QString::number(ui->comboBox_m_w->currentIndex()) + " ";

        QString sql_text;
        sql_text =  "UPDATE child_data SET " + insert_text + " WHERE id=" + QString::number(id);
        qDebug() << "sql = " << sql_text;
        QMessageBox::information(this, "修改成功", "修改成功", "确认", "取消");
        this->close();
    }
}

check::~check()
{
    delete ui;
}
